package com.sunwoda.springbootredis.controller;

import com.sunwoda.springbootredis.entity.User;
import com.sunwoda.springbootredis.redislock.LockVersion1;
import com.sunwoda.springbootredis.redislock.LockVersion2;
import com.sunwoda.springbootredis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @program: springbootredis
 * @description: redis实现分布式锁
 * @author: SGR
 * @create: 2021-09-14 09:19
 **/
@RestController
@RequestMapping(value = "/lock")
public class LockController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
    * @Description: redis分布式锁version1
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-14
    */
    @GetMapping(value = "version1")
    public String lockVersion1(){
        LockVersion1 version1 = new LockVersion1();
        String s = version1.deductStock();
        return s;
    }

    /**
     * @Description: redis分布式锁version1
     * @Param:
     * @return:
     * @Author: SGR
     * @Date: 2021-09-14
     */
    @GetMapping(value = "version2")
    public String lockVersion2(){
        LockVersion2 version2 = new LockVersion2();
        String s = version2.lock();
        return s;
    }

    /**
    * @Description: 更新数据
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-15
    */
    @PutMapping("/update")
    public void update(@RequestBody User re){
        int update = this.userService.update(re);
    }

    /**
     * @Description: 更新数据
     * @Param:
     * @return:
     * @Author: SGR
     * @Date: 2021-09-15
     */
    @PutMapping("/update2")
    public void update2(@RequestBody User re){
        User update = this.userService.update2(re);
    }

}
